package com.zhangxu.microservice.system.dao.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangxu.microservice.system.dao.condition.ApplicationCondition;
import com.zhangxu.microservice.system.dao.entity.ApplicationDO;
import com.zhangxu.microservice.system.dao.mapper.ApplicationMapper;
import com.zhangxu.microservice.system.dao.service.ApplicationDao;
import org.springframework.stereotype.Repository;

import java.io.Serializable;

/**
 * 系统管理DAO实现类
 */
@Repository
public class ApplicationDaoImpl extends ServiceImpl<ApplicationMapper, ApplicationDO> implements ApplicationDao {

    @Override
    public ApplicationDO getByCode(String applicationCode) {
        return this.lambdaQuery()
                .eq(ApplicationDO::getCode, applicationCode)
                .eq(ApplicationDO::getDeleteTime, 0L)
                .one();
    }

    @Override
    public ApplicationDO getById(Serializable id) {
        return this.lambdaQuery()
                .eq(ApplicationDO::getId, id)
                .eq(ApplicationDO::getDeleteTime, 0L)
                .one();
    }

    @Override
    public IPage<ApplicationDO> page(ApplicationCondition condition) {
        condition.checkPage();
        return this.lambdaQuery()
                .like(StrUtil.isNotBlank(condition.getName()), ApplicationDO::getName, condition.getName())
                .eq(StrUtil.isNotBlank(condition.getCode()), ApplicationDO::getCode, condition.getCode())
                .eq(condition.getStatus() != null, ApplicationDO::getStatus, condition.getStatus())
                .eq(condition.getOwnerId() != null, ApplicationDO::getOwnerId, condition.getOwnerId())
                .eq(ApplicationDO::getDeleteTime, 0L)
                .orderByAsc(ApplicationDO::getSort)
                .orderByDesc(ApplicationDO::getCreateTime)
                .page(condition.getIpage());
    }
}